/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.util;

import com.ceair.flight.refund.location.model.FlightOrderDO;
import com.ceair.flight.refund.location.model.OrderDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 *
 * @author youguilin
 */
public final class OrderUtil {

    private static final Logger LOG = LoggerFactory.getLogger(OrderUtil.class);
    private final OrderDO entity;

    public OrderUtil(OrderDO order) {
        if (order == null) {
            throw new IllegalArgumentException("Big order entity is null");
        }
        LOG.info("Create big order util by big order entity and order No is {}", order.getOrderNo());
        entity = order;
    }

    public OrderDO getEntity() {
        return entity;
    }

    /**
     * 获取所有改期前的机票订单
     *
     * @param flightOrder
     * @return
     */
    public List<FlightOrderDO> getRescheduleFlightOrders(FlightOrderDO flightOrder) {
        FlightOrderUtil flightOrderUtil = new FlightOrderUtil(flightOrder);
        boolean isReschedule = flightOrderUtil.isRescheduleFlightOrder();
        if (!isReschedule) {
            return null;
        }
        Set<FlightOrderDO> allFlightOrders = entity.getFlightOrders();
        LOG.debug("Flight order {} has total {} flight orders under big order {}", flightOrder.getFlightOrderNo(), allFlightOrders.size(), entity.getOrderNo());
        //改期订单最大数(去除最后一个已改期订单)为总订单数减去一
        List<FlightOrderDO> recheduleOrders = new ArrayList<>(allFlightOrders.size() - 1);
        String oldId = flightOrder.getOldRecId();
        while (oldId != null && !oldId.isEmpty()) {
            FlightOrderDO recheduleOrder = getFlightOrderDO(allFlightOrders, oldId);
            if (recheduleOrder == null) {
                LOG.error("Reschedule flight order not found with id {}", oldId);
                break;
            } else {
                recheduleOrders.add(recheduleOrder);
                oldId = recheduleOrder.getOldRecId();
            }
        }
        return recheduleOrders;
    }

    /**
     * 获取改期机票的原始机票订单
     *
     * @param flightOrder
     * @return
     */
    public FlightOrderDO getOrignalFlightOrder(FlightOrderDO flightOrder) {
        if (flightOrder == null) {
            throw new IllegalArgumentException("Get original flight order with null source flight order");
        }
        List<FlightOrderDO> allRescheduleFlightOrders = getRescheduleFlightOrders(flightOrder);
        if (allRescheduleFlightOrders == null || allRescheduleFlightOrders.isEmpty()) {
            return null;
        }
        //最后一个机票订单为原始机票订单
        return allRescheduleFlightOrders.get(allRescheduleFlightOrders.size() - 1);
    }

    private FlightOrderDO getFlightOrderDO(Set<FlightOrderDO> allFlightOrders, String flightOrderId) {
        for (FlightOrderDO flightOrder : allFlightOrders) {
            if (flightOrder.getFlightOrderNo().equals(flightOrderId)) {
                return flightOrder;
            }
        }
        return null;
    }

}
